package com.zyl.file.util;


import cn.hutool.core.util.ObjectUtil;
import com.jcraft.jsch.ChannelSftp;
import org.springframework.util.ObjectUtils;

import javax.activation.MimetypesFileTypeMap;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 判断文件类型工具类
 *
 * @author panjiazhu
 * @date 2022/7/13
 */
public class FileCheckUtils {

    private static MimetypesFileTypeMap mtftp;

    /**
     * 判断是否图片
     *
     * @param file
     * @return
     */
    public static boolean isImage(File file) {
        mtftp = new MimetypesFileTypeMap();
        mtftp.addMimeTypes("image png tif jpg jpeg bmp");
        String mimetype = mtftp.getContentType(file);
        String type = mimetype.split("/")[0];
        return "image".equals(type);
    }

    public static void main(String[] args) {
        File file = new File("C:\\duolaam.png");
        System.out.println(file.getName());
        System.out.println(isImage(file));
    }

    public static boolean isImage(List<String> urlStr) {
        for (String s : urlStr) {
            InputStream inputStream1 = FileUtils.getInputStreamFromUrl(s);
            try {
                BufferedImage image = ImageIO.read(inputStream1);
                if (ObjectUtils.isEmpty(image)) {
                    return false;
                }
            } catch (IOException e) {
                return true;
            }
        }
        return true;
    }

    /**
     * 判断是否是图片
     * @param inputStream 输入流
     * @return boolean
     */
    public static boolean isImage(InputStream inputStream) {
        try {
            BufferedImage image = ImageIO.read(inputStream);
            if (ObjectUtil.isNotEmpty(image)) {
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }

    public static boolean isImage(String urlStr, String host, Integer port, String username, String password) {
        ChannelSftp connect = null;
        try {
             connect = FtpUtils.getConnect(host, port, username, password);
            InputStream inputStream = connect.get(urlStr);
            BufferedImage image = ImageIO.read(inputStream);
            if (ObjectUtils.isEmpty(image)) {
                return false;
            }
        } catch (Exception e) {
            return true;
        }finally {
            connect.disconnect();
        }
        return true;
    }
}
